﻿using AWSV2.Services;
using Common.Utility.AJ.Extension;
using Common;
using NPOI.SS.Formula.Functions;
using Stylet;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using Common.Models;
using Common.Utility;

namespace AWSV2.ViewModels
{
    public class PicListViewModel : Screen
    {
        public string TextInfo { get; set; }
        public string ErrorInfo { get; set; }
        public System.Windows.Media.Brush StateForeground { get; set; } = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#000000"));


        public string[] _dirctories;
        public string[] Dirctories 
        { get {
                return _dirctories;
            }
            set
            {
                SetAndNotify(ref _dirctories, value);
            }
        }

        //加载其他窗口
        private IWindowManager _windowManager;
        /// <summary>
        /// 创建窗体
        /// </summary>
        /// <param name="dirctoryPath">图片的文件夹路径</param>
        public PicListViewModel(string[] dirctories)
        {
            _windowManager = AJStyletService.Instance().WindowMgr;
            Dirctories = dirctories;
            this.Closed += ConfirmWithChargeViewModel_Closed;
        }

        public static void Show(WeighingRecord record)
        {
            var root = Common.Utility.SettingsHelper.LPRSavePath;

            if (record == null)
            {
                if (Directory.Exists(root)) Process.Start(new ProcessStartInfo
                {
                    FileName = root,
                    UseShellExecute = true,
                });

                return;

            }

            var ch = record.Ch;
            if (string.IsNullOrEmpty(ch))
            {
                MessageBoxExt.Alert(I18NService.Translate(LangKeys.未找到该记录的图片信息));
                return;
            }

            var dirs = new string[2];

            dirs[0] = Path.Combine(root, ch);
            dirs[1] = Path.Combine(dirs[0], record.Bh);

            if (!Directory.Exists(dirs[0]) && !Directory.Exists(dirs[1]))
            {
                MessageBoxExt.Alert(I18NService.Translate(LangKeys.未找到该记录的图片信息));
                return;
            }

            AJStyletService.Instance().WindowMgr.ShowDialog(new PicListViewModel( dirs));
        }

        private void ConfirmWithChargeViewModel_Closed(object sender, CloseEventArgs e)
        {
            //为了不让这段代码独守空房，索性加个注释吧。。。
        }


        private void BackupWindow_Closed(object sender, CloseEventArgs e)
        {
            this.RequestClose(true);
        }

        public void Cancel()
        {
            this.RequestClose(false);
        }
    }
}
